home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / HOTJAVA_ / HOTJAVA / CLASSSRC / BROWSER / SELECT~1.JAV < prev    next >
Encoding:
Text File  |  1995-08-11  |  3.0 KB  |  117 lines

  1. /*
  2.  * @(#)SelectTagRef.java    1.8 95/03/17 Jonathan Payne
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package browser;
  21.  
  22. import browser.forms.FormItem;
  23. import browser.forms.select;
  24. import awt.Component;
  25. import awt.NativeDisplayItem;
  26. import net.www.html.Tag;
  27. import net.www.html.TagRef;
  28.  
  29. /** A SelectTagRef is just like an InputTagRef.  We just stick in
  30.     the "type" field in the TagRef for them. */
  31.  
  32. class SelectTagRef extends InputTagRef {
  33.     TagRef  lastOptionRef = null;
  34.  
  35.     public SelectTagRef(Tag t, int pos, boolean isEnd) {
  36.     super(t, pos, isEnd);
  37.     }
  38.  
  39.     public void newOption(byte text[], TagRef ref) {
  40.     if (lastOptionRef != null) {
  41.         String  option = new String(text, 0, lastOptionRef.pos, ref.pos - lastOptionRef.pos);
  42.  
  43.         ((select) item).addOption(option,
  44.                       lastOptionRef.getAttribute("value"),
  45.                       lastOptionRef.getAttribute("selected") != null);
  46.     }
  47.     lastOptionRef = ref;
  48.     }
  49.  
  50.     public void finish() {
  51.     ((select) item).finish();
  52.     }
  53.  
  54.     /* We override this here so that we can make sure we have the
  55.        actual size. */
  56.     public void addDisplayItem(WRFormatter f) {
  57.     if (item != null) {
  58.         NativeDisplayItem    ndi = (NativeDisplayItem) item.getDisplayItem();
  59.  
  60.         if (ndi != null) {
  61.         Component   c = ndi.getComponent();
  62.  
  63.         ndi.resize(c.width, c.height);
  64.         }
  65.         super.addDisplayItem(f);
  66.     }
  67.     }
  68.  
  69.     public void apply(WRFormatter f) {
  70.     try {
  71.         FormTagRef        form = f.formContext();
  72.         SelectTagRef    select = (SelectTagRef) form.getInputItem();
  73.  
  74.         if (!isEnd) {
  75.         if (item == null) {
  76.             item = buildFormItem(f);
  77.         }
  78.         form.setInputItem(this);
  79.         lastOptionRef = null;
  80.         f.stopRendering();
  81.         } else {
  82.         select.newOption(f.doc.getText(), this);
  83.         select.finish();
  84.         form.setInputItem(null);
  85.         if (select != null) {
  86.             select.addDisplayItem(f);
  87.         }
  88.         f.startRendering();
  89.         }
  90.     } catch (Exception e) {
  91.         e.printStackTrace();
  92.         System.out.println("Error: " + e + " while processing " + this.toExternalForm());
  93.     }
  94.     }
  95.  
  96.     /*
  97.      * Unlike input tags, select tags can only have select items
  98.      * inside of them.
  99.      */
  100.     FormItem buildFormItem(WRFormatter f) {
  101.     FormItem    i = null;
  102.     FormTagRef  form;
  103.  
  104.     form = f.formContext();
  105.     if (form != null) {
  106.         i = (FormItem) new("browser.forms.select");
  107.         i.initialize(f, form, this);
  108.         form.addInputItem(i);
  109.     }
  110.     return i;
  111.     }
  112.  
  113.  
  114.  
  115.  
  116. }
  117.